/*
	       File: frmProperties.cs
	Create Date: 2007-08-16
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public partial class frmProperties : Form
    {
        #region Private member fields
        #endregion

        #region Constructors and cleanup methods
        public frmProperties()
        {
            InitializeComponent();
            lstProperties.DisplayMember = "Display";
        }
        #endregion

        #region Private implementation methods
        private void DoUpdateProperties()
        {
            if (null != UpdateProperties)
            {
                List<Property> properties = new List<Property>();
                foreach (Property prop in lstProperties.Items)
                {
                    properties.Add(prop);
                }
                UpdateProperties(properties);
            }
        }
        #endregion

        #region Events handlers
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnPropertyUpdate_Click(object sender, EventArgs e)
        {
            if (-1 == lstProperties.SelectedIndex)
            {
                return;
            }

            Property property = (Property) lstProperties.SelectedItem;
            property.Name = txtName.Text.Replace(' ', '_');
            property.Value = txtValue.Text;
            int index = lstProperties.SelectedIndex;

            lstProperties.Items.Remove(lstProperties.SelectedItem);
            lstProperties.Items.Insert(index, property);

        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int index = lstProperties.Items.Add(new Property("new_property", ""));
            lstProperties.SelectedIndex = index;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (-1 == lstProperties.SelectedIndex)
            {
                return;
            }
            lstProperties.Items.Remove(lstProperties.SelectedItem);
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DoUpdateProperties();
            this.Close();
        }
        private void lstProperties_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (-1 == lstProperties.SelectedIndex)
            {
                return;
            }

            Property property = (Property) lstProperties.SelectedItem;
            txtName.Text = property.Name;
            txtValue.Text = property.Value;
        }
        #endregion

        #region Public interface
        public void LoadFileProperties(List<ConfigurationFileTypeParameter> parameters)
        {
            lstProperties.DisplayMember = "Display";
            if (null == parameters)
            {
                parameters = new List<ConfigurationFileTypeParameter>();
            }
            foreach (ConfigurationFileTypeParameter parameter in parameters)
            {
                Property property = new Property(parameter.Name, parameter.Value);
                lstProperties.Items.Add(property);
            }
        }
        public void LoadDirectoryProperties(List<ConfigurationDirectoryParameter> parameters)
        {
            lstProperties.DisplayMember = "Display";
            if (null != parameters)
            {
                foreach (ConfigurationDirectoryParameter parameter in parameters)
                {
                    Property property = new Property(parameter.Name, parameter.Value);
                    lstProperties.Items.Add(property);
                }
            }
        }

        public delegate void UpdatePropertiesDelegate(List<Property> properties);
        public event UpdatePropertiesDelegate UpdateProperties;
        #endregion
    }
}